﻿using Coldairarrow.Entity.Base_leasesystem_merchant;
using Coldairarrow.Entity.Base_leasesystem_merchantpaychannel;
using Coldairarrow.Entity.Base_leasesystem_service;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_merchantpaychannel
{
    public class leasesystem_merchantpaychannelBusiness : BaseBusiness<leasesystem_merchantpaychannel>, Ileasesystem_merchantpaychannelBusiness, ITransientDependency
    {
        public leasesystem_merchantpaychannelBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<leasesystem_merchantpaychannel>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_merchantpaychannel>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_merchantpaychannel, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<object> GetDataListOver(PageInput<ConditionDTO> input)
        {
            var obj = Db.GetIQueryable<leasesystem_merchantpaychannel>()
                .Join(Db.GetIQueryable<leasesystem_merchant>(), a => a.MID, b => b.Id, (a, b) => new
                {
                    a.Id,
                    a.MID,
                    a.SID,
                    a.ChannelId,
                    a.ChannelName,
                    a.CreateTime,
                    a.CreatorId,
                    a.DayMaxPayAmount,
                    a.MaxPayAmount,
                    a.OpenStatus,
                    b.customerName
                }).Join(Db.GetIQueryable<leasesystem_service>(), a => a.SID, b => b.Id, (a, b) => new
                {
                    a.Id,
                    a.MID,
                    a.SID,
                    a.ChannelId,
                    a.ChannelName,
                    a.CreateTime,
                    a.CreatorId,
                    a.DayMaxPayAmount,
                    a.MaxPayAmount,
                    a.OpenStatus,
                    a.customerName,
                    b.ServiceName
                });
            var result = new{
                Data = obj.OrderBy($@"{input.SortField} {input.SortType}").Skip((input.PageIndex - 1) * input.PageRows)
                      .Take(input.PageRows),
                Total = obj.Count()
            };
            return result;
        }

        public async Task<leasesystem_merchantpaychannel> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_merchantpaychannel data)
        {
            var merpay  = Db.GetIQueryable<leasesystem_merchantpaychannel>().Where(a => a.MID == data.MID && a.SID == data.SID).ToList();
            for (int i = 0; i < merpay.Count; i++)
            {
                merpay[i].OpenStatus = 0;
            }
            await UpdateAsync(merpay);
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_merchantpaychannel data)
        {
            var merpay = Db.GetIQueryable<leasesystem_merchantpaychannel>().Where(a => a.MID == data.MID && a.SID == data.SID && a.Id != data.Id).ToList();
            for (int i = 0; i < merpay.Count; i++)
            {
                merpay[i].OpenStatus = 0;
            }
            await UpdateAsync(merpay);
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public object GetDataByM_SID(List<string> ids) 
        {
            var obj = Db.GetIQueryable<leasesystem_merchantpaychannel>().Where(a => a.MID == ids[0] && a.SID == ids[1] && a.OpenStatus ==1);
            return obj;
        }
        #endregion

        #region 私有成员

        #endregion
    }
}